#include "stdafx.h"
#include "HttpServer.h"
#include <malloc.h>

#include "HttpFiberClient.h"

HttpServer::HttpServer(void)
{
}


HttpServer::~HttpServer(void)
{
}


class HttpDefaultAllocator : public HttpBaseAllocator
{
	virtual void* Alloc(size_t size)
	{
		return malloc(size);
	}

	virtual void Free(void* m)
	{
		return free(m);
	}
};

auto_ptr<HttpBaseAllocator> HttpServer::s_allocator = auto_ptr<HttpBaseAllocator>();




void* HttpServer::Alloc(size_t size)
{
	if(!s_allocator.get())
	{
		HTTPWARN("No allocator set for HttpServer. Falling back to default");
		s_allocator.reset(new HttpDefaultAllocator);
	}

	return s_allocator->Alloc(size);
}

void HttpServer::Free(void*)
{
	if(!s_allocator.get())
	{
		HTTPFATAL("No allocator set for HttpServer and Free() was called.");
		abort();
	}
}

bool HttpServer::ReadConfig(const _TCHAR* filename)
{
	if(!m_config.ReadConfig(filename))
	{
		return false;
	}

	SetMaxIocpThreads(m_config.m_performance.iocpHandlers);

	return true;
}


BaseFiberClientHandler* HttpServer::CreateFiberClientHandler()
{
	return new HttpFiberClient(this);
}
